package com.itwlj.controller.user;

import com.itwlj.constant.StatusConstant;
import com.itwlj.dto.GoodsPageQueryDTO;
import com.itwlj.entity.Category;
import com.itwlj.entity.SubCategory;
import com.itwlj.entity.Goods;
import com.itwlj.result.PageResult;
import com.itwlj.result.Result;
import com.itwlj.service.CategoryService;
import com.itwlj.service.SubCategoryService;
import com.itwlj.service.GoodsService;
import com.itwlj.vo.CategoryDetailVO;
import com.itwlj.vo.CategoryBreadcrumbVO;
import com.itwlj.vo.SubCategoryDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取一级分类数据
 */
@RestController
@RequestMapping("/category")
@Api(tags = "C端分类相关接口")
@Slf4j
public class CategoryDetailController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SubCategoryService subCategoryService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 根据分类id获取子分类和商品信息
     *
     * @param id
     * @return
     */
    @GetMapping
    @ApiOperation("根据分类id获取子分类和商品信息")
    public Result<CategoryDetailVO> getCategoryDetail(Long id) {
        log.info("根据分类id获取子分类和商品信息{}：", id);
        //获取分类对象
        Category category = categoryService.getById(id);

        //获取子分类信息
        List<SubCategory> subCategoryList = subCategoryService.getByCategoryId(id);
        List<SubCategoryDetailVO> subCategoryDetailVOList = new ArrayList<>();
        subCategoryList.forEach(subCategory -> {
            List<Goods> goodsList = goodsService.getBySubCategoryId(subCategory.getId());
            goodsList = goodsList.size() > 5 ? goodsList.subList(0, 5) : goodsList;
            SubCategoryDetailVO subCategoryDetailVO = SubCategoryDetailVO.builder()
                    .id(subCategory.getId())
                    .name(subCategory.getName())
                    .picture(subCategory.getPicture())
                    .goodsList(goodsList)
                    .build();

            subCategoryDetailVOList.add(subCategoryDetailVO);
        });

        CategoryDetailVO categoryDetailVO = CategoryDetailVO.builder()
                .id(category.getId())
                .name(category.getName())
                .subCategoryDetailList(subCategoryDetailVOList)
                .build();
        return Result.success(categoryDetailVO);
    }

    /**
     * 根据分类id获取面包屑导航
     *
     * @param id
     * @return
     */
    @GetMapping("/sub/breadcrumb")
    @ApiOperation("根据分类id获取面包屑导航")
    public Result<CategoryBreadcrumbVO> getCategoryBreadcrumb(Long id) {
        log.info("根据分类id获取面包屑导航：{}", id);
        //获取分类信息
        Category category = categoryService.getById(id);
        //获取子分类信息
        SubCategory subCategory = subCategoryService.getById(id);

        CategoryBreadcrumbVO categoryBreadcrumbVO = CategoryBreadcrumbVO.builder()
                .categoryId(category.getId())
                .categoryName(category.getName())
                .subCategoryName(subCategory.getName())
                .build();

        return Result.success(categoryBreadcrumbVO);
    }

    /**
     * 商品分页查询
     *
     * @param goodsPageQueryDTO
     * @return
     */
    @PostMapping("sub/goods")
    @ApiOperation("商品分页查询")
    public Result<PageResult> goodsPageQuery(@RequestBody GoodsPageQueryDTO goodsPageQueryDTO) {
        log.info("商品分页查询：{}", goodsPageQueryDTO);
        goodsPageQueryDTO.setStatus(StatusConstant.ENABLE);
        PageResult pageResult = goodsService.pageQuery(goodsPageQueryDTO);
        return Result.success(pageResult);
    }
}
